<?php

class ProductPhotoHelper extends UploadHelper {

    protected function initialize() {
        parent::initialize();
    }

    protected function handle_form_data($file, $index) {
        $file->product_id = @$_REQUEST['product_id'][$index];
        $file->photo_type = @$_REQUEST['photo_type'][$index];
    }

    protected function handle_file_upload($uploaded_file, $name, $size, $type, $error, $index = null, $content_range = null) {
        $file = parent::handle_file_upload(
                        $uploaded_file, $name, $size, $type, $error, $index, $content_range
        );
        if (empty($file->error)) {
            $productPhoto = new ProductPhoto();
            $productPhoto['image_name'] = $file->name;
            $productPhoto['product_id'] = $file->product_id ? $file->product_id : 0;
            $productPhoto['type'] = $file->photo_type == "on" ? 1 : 0;
            $productPhoto['created_at'] = AppTools::getDbTime();
            $productPhoto['created_user_id'] = sfContext::getInstance()->getUser()->getId();
            $productPhoto->save();

            $file->id = $productPhoto['id'];
        }
        return $file;
    }

    protected function trim_file_name($file_path, $name, $size, $type, $error, $index, $content_range) {
        return uniqid() . '.' . pathinfo($name, PATHINFO_EXTENSION);
    }

    protected function set_additional_file_properties($file) {
        parent::set_additional_file_properties($file);
    }

    protected function get_file_object($fileName) {
        if ($this->is_valid_file_object($fileName)) {
            $file = new stdClass();
            $file->name = $fileName;
            $file->size = $this->get_file_size(
                    $this->get_upload_path($fileName)
            );
            $file->url = $this->get_download_url($file->name);
            foreach ($this->options['image_versions'] as $version => $options) {
                if (!empty($version)) {
                    if (is_file($this->get_upload_path($fileName, $version))) {
                        $file->{$version . 'Url'} = $this->get_download_url(
                                $file->name, $version
                        );
                    }
                }
            }
            $this->set_additional_file_properties($file);
            return $file;
        }
        return null;
    }

    protected function get_file_objects($iteration_method = 'get_file_object') {
        if ($_SERVER['REQUEST_METHOD'] === 'GET') {
            $productId = isset($_GET['pid']) ? $_GET['pid'] : 0;
            $productPhotoIds = isset($_GET['pp']) ? $_GET['pp'] : 0;

            $files = array();
            if ($productId) {
                $arr = ProductPhotoTable::getList($productId);
                foreach ($arr as $item) {
                    $file = $this->get_file_object($item['image_name']);
                    if ($file) {
                        $file->id = $item['id'];
                        $file->product_id = $item['product_id'];
                        $file->photo_type = $item['type'];
                        $files[] = $file;
                    }
                }
            }

            if ($productPhotoIds) {
                $productPhotoIds = explode(',', $productPhotoIds);
                $arr = ProductPhotoTable::retrieveByPK($productPhotoIds);
                foreach ($arr as $item) {
                    $file = $this->get_file_object($item['image_name']);
                    if ($file) {
                        $file->id = $item['id'];
                        $file->product_id = $item['product_id'];
                        $file->photo_type = $item['type'];
                        $files[] = $file;
                    }
                }
            }
        }

        $upload_dir = $this->get_upload_path();
        if (!is_dir($upload_dir)) {
            return array();
        }
        return $files;
//        return array_values(array_filter());
//        return array_values(array_filter(array_map(array($this, $iteration_method), scandir($upload_dir))));
    }

    public function delete($print_response = true) {
        $response = parent::delete(false);
        foreach ($response as $name => $deleted) {
            if ($deleted) {
                $photo = ProductPhotoTable::retrieveByName($name);
                if ($photo) {
                    $photo['is_active'] = 0;
                    $photo->save();
                }
            }
        }
        return $this->generate_response($response, $print_response);
    }

}
